class MyBlockingQueue{
    private String[] data = null;
    private int head = 0;
    private int tail = 0;
    private int size = 0;

    public MyBlockingQueue(int capacity) {
        data = new String[capacity];
    }
    public void put(String elem) throws InterruptedException {
        synchronized (this){
            //此处最好使用while
            //否则notifyAll的时候，该线程从wait中被唤醒
            //但是紧接者并未抢占到锁，当锁被抢占的时候，可能又已经队列满了
            //就只能继续等待
            while (size >= data.length){
                //队列满了，需要阻塞
                //return;
                this.wait();
            }
            data[tail] = elem;
            tail++;
            if(tail >= data.length){
                tail = 0;
            }
            size++;
            this.notify();
        }
    }
    public String take() throws InterruptedException {
        synchronized (this){
            while (size == 0){
                //队列空了，需要阻塞
                //return null;
                this.wait();
            }
            String ret = data[head];
            head++;
            if(head>= data.length){
                head = 0;
            }
            size--;
            this.notify();
            return ret;
        }

    }
}
public class Demo {
    public static void main(String[] args) {

    }
}
